// pages/hb/hb.js
const app = getApp();

import { getHbList, getInviterHbList } from "../../api/user";
import { getSharePng } from "../../config";
import { getinviteKey, getToken } from "../../utils/storage";
let user_name = "";
const hbTypeClass = {
  流: "liushui-logo",
  新: "new-logo",
  钻: "zuan-logo",
  铂: "bo-logo",
  金: "jin-logo",
  银: "yin-logo",
  墙: "qiang-logo",
  邀: "invite-logo",
};

Page({
  /**
   * 组件的初始数据
   */
  data: {
    my_invite_key: getinviteKey(),
    tabItems: ["邀请红包", "未使用", "已到账", "已过期"],
    currentIndex: "1",
    InviterHbList: [],
    hbList: [],
    len: 10,
    limitShowCount: 30, // 最大显示条数
    totalMoney: 0,
    notUseMoney: 0,
    expireMoney: 0,
  },

  onLoad: function (options) {
    const currentIndex = options.hbtype || getApp().globalData.hbtype || "1";
    this.setData({
      currentIndex: currentIndex,
    });
    this.tabController = this.selectComponent("#tabController");
    this.tabController.initIndex(Number(this.data.currentIndex) - 1);
  },
  onShow: function () {
    if (!getToken()) {
      return;
    }
    this.setData({
      currentIndex: getApp().globalData.hbtype || "1",
    });
    if (this.data.currentIndex == 1) {
      this.InviterHbList();
      this.HbListTop(this.data.currentIndex);
    } else {
      this.HbList(this.data.currentIndex - 1);
    }
    // this.HbList(this.data.currentIndex)
    user_name = getApp().globalData.user_name || "著名主持人";
    this.setData({
      my_invite_key: getinviteKey(),
    });
  },
  InviterHbList: function () {
    getInviterHbList({})
      .then((res) => {
        res.data.list.forEach((item) => {
          item.class = hbTypeClass[item.classification];
        });
        this.setData({
          hbList: res.data.list,
        });
      })
      .catch((err) => {});
  },
  HbListTop: function (type) {
    getHbList({
      query_type: type,
    }).then((res) => {
      this.setData({
        totalMoney: res.data.yiLingHb.bonus,
        notUseMoney: res.data.keYongHb.bonus,
        expireMoney: res.data.guoQiHb.bonus,
      });
    });
  },
  HbList: function (type) {
    const { limitShowCount, count } = this.data;

    getHbList({
      query_type: type,
    }).then((res) => {
      const len = res.data.list.length;

      if (len > limitShowCount) {
        res.data.list = res.data.list.filter(
          (item, index) => index < limitShowCount
        );
      }
      res.data.list.forEach((item) => {
        item.class = hbTypeClass[item.classification];
      });
      this.setData({
        hbList: res.data.list,
        len: len,
        totalMoney: res.data.yiLingHb.bonus,
        notUseMoney: res.data.keYongHb.bonus,
        expireMoney: res.data.guoQiHb.bonus,
      });
    });
  },
  itemClick: function (e) {
    let currentIndex = e.detail + 1 + "";

    this.setData({
      currentIndex,
    });
    if (currentIndex == 1) {
      this.InviterHbList("inviter", "1", "3");
    } else {
      this.HbList(currentIndex - 1);
    }
  },
  onReachBottom() {
    wx.showToast({
      title: "已经到底了",
      icon: "none",
    });
  },
  /**
   * 组件的方法列表
   */
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    getApp().globalData.isNotPageShow = true;
    return {
      title: `${user_name || ""}邀请您加入嗨喵悦动`,
      path: "/pages/regist/regist?share_invite_key=" + this.data.my_invite_key,
      imageUrl: getSharePng(),
    };
  },
});
